package com.example.luckybag.utils

import android.app.ActivityManager
import android.content.Context
import android.provider.Settings

import android.text.TextUtils
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.edit
import com.example.luckybag.LuckyBagApp

/**
 * 文件名： <br/>
 * 描述：🌝🌝🌝
 *
 * @author wulinran
 * @since 2023/2/9 09:46
 */
class Utils {
    companion object {
        /**
         * 判断服务是否开启
         * @param context
         * @param ServiceName
         * 服务的完整路径(例:com.example.service)
         * @return
         */
        fun isServiceRunning(context: Context, ServiceName: String?): Boolean {
            if (TextUtils.isEmpty(ServiceName)) {
                return false
            }
            val myManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val runningService =
                myManager.getRunningServices(30) as ArrayList<ActivityManager.RunningServiceInfo>
            for (i in 0 until runningService.size) {
                if (runningService[i].service.className == ServiceName) {
                    return true
                }
            }
            return false
        }

        fun enabled(context: Context,service: String): Boolean {
            var ok = 0
            try {
                ok = Settings.Secure.getInt(
                    context.applicationContext.contentResolver,
                    Settings.Secure.ACCESSIBILITY_ENABLED
                )
            } catch (e: Settings.SettingNotFoundException) {
            }

            val ms: TextUtils.SimpleStringSplitter = TextUtils.SimpleStringSplitter(':')
            if (ok == 1) {
                val settingValue: String = Settings.Secure.getString(
                    context.applicationContext.contentResolver,
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
                )
                ms.setString(settingValue)
                while (ms.hasNext()) {
                    val accessibilityService: String = ms.next()
                    if (accessibilityService.equals(service, true)) {
                        return true
                    }

                }


            }

            return false
        }

        fun canShowNotification(context: Context?): Boolean {
            return NotificationManagerCompat.from(context!!)
                .areNotificationsEnabled()
        }


        fun luckyPackageSwitch(): Boolean {
            return LuckyBagApp.sIntance?.luckyPackageSwitch?.getBoolean("luckyPackageSwitch",true) == true
        }

        fun luckyPackageSwitchChange(switch:Boolean) {
            LuckyBagApp.sIntance?.luckyPackageSwitch?.edit {
                putBoolean("luckyPackageSwitch",switch)
            }
        }

    }
}